package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import orm.ChildrenBehaviorPurpose;
import orm.IHomeworkDomainInfo;
import util.DBHelper;

public class IHomeworkDomainInfoDAO {
	
	public void insertIHomeworkDomainInfo(Statement statement, 
			ArrayList<IHomeworkDomainInfo> ihwdiArr, String tpID) throws SQLException {
		IHomeworkDomainInfo ihwdi = null;
		for (int i = 0; i < ihwdiArr.size(); i++) {
			ihwdi = ihwdiArr.get(i);
			statement.execute("insert into ihomeworkdomaininfo values('" + tpID + "'," +
					ihwdi.getDomainID() + ", " + ihwdi.getAimID() + ", '" + ihwdi.getTaskAim() +
					"', '" + ihwdi.getContentMaterial() + "', '" + ihwdi.getLearnToHave() + 
					"', '" + ihwdi.getGuidingMeans() + "', " + ihwdi.getCompleteEntering() + ")");
		}
	}
	
	public void updateIHomeworkDomainInfo(Statement statement,
			ArrayList<IHomeworkDomainInfo> ihwdiArr, String tpID) throws SQLException {
		IHomeworkDomainInfo ihwdi = null;
		for (int i = 0; i < ihwdiArr.size(); i++) {
			ihwdi = ihwdiArr.get(i);
			StringBuffer sqlStr = new StringBuffer();
			sqlStr.append("update ihomeworkdomaininfo set TaskAim = '");
			sqlStr.append(ihwdi.getTaskAim() + "', ContentMaterial = '" + ihwdi.getContentMaterial());
			sqlStr.append("', LearnToHave = '" + ihwdi.getLearnToHave() + "', GuidingMeans = '" );
			sqlStr.append(ihwdi.getGuidingMeans() + "',CompleteEntering = " + ihwdi.getCompleteEntering() + " where TPID = '");
			sqlStr.append(tpID + "' and DomainID = " + ihwdi.getDomainID() + " and AimID = " + ihwdi.getAimID() + "");
			
			statement.execute(sqlStr.toString());
		}
	}
	
	public void deleteIHomeworkDomainInfo(Statement statement, String tpID) throws SQLException {
		statement.execute("delete from ihomeworkdomaininfo where TPID='" + tpID + "'");
	}
	
	public ArrayList<IHomeworkDomainInfo> getIHWDIByTPID(String tpID) throws SQLException {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		ArrayList<IHomeworkDomainInfo> al = new ArrayList<IHomeworkDomainInfo>();
		IHomeworkDomainInfo ihwdi = null;
		conn = DBHelper.getConnection();
		stmt = conn.createStatement();
		rs = stmt.executeQuery("select * from ihomeworkdomaininfo where TPID='" + tpID + "'");
		
		while (rs.next()) {
			ihwdi = new IHomeworkDomainInfo();
			int i = 1;
			ihwdi.setTPID(rs.getString(i++));
			ihwdi.setDomainID(rs.getInt(i++));
			ihwdi.setAimID(rs.getInt(i++));
			ihwdi.setTaskAim(rs.getString(i++));
			ihwdi.setContentMaterial(rs.getString(i++));
			ihwdi.setLearnToHave(rs.getString(i++));
			ihwdi.setGuidingMeans(rs.getString(i++));
			ihwdi.setCompleteEntering(rs.getInt(i++));
			al.add(ihwdi);
		}
		
		rs.close();
		stmt.close();
		conn.close();
		
		return al;
	}
}
